<?php

namespace CMS\Model;

use Think\Model;

/**
 * 导航模型
 */
class Channel extends Model
{
    protected $name = 'cms_channel';
    protected $tableName = 'channel';

    protected $_validate = array(
        array(
            'title',
            'require',
            '标题不能为空',
            self::MUST_VALIDATE,
            'regex',
            self::MODEL_BOTH
        ),
        array(
            'url',
            'require',
            'URL不能为空',
            self::MUST_VALIDATE,
            'regex',
            self::MODEL_BOTH
        )
    );

    protected $_auto = array(
        array(
            'create_time',
            NOW_TIME,
            self::MODEL_INSERT
        ),
        array(
            'update_time',
            NOW_TIME,
            self::MODEL_BOTH
        ),
        array(
            'status',
            '1',
            self::MODEL_INSERT
        )
    );
    
    /**
     * 获取导航列表，支持多级导航
     * 
     * @param boolean $field
     *            要列出的字段
     * @return array 导航树
     */
    public function lists($field = true)
    {
        $map = array(
            'status' => 1
        );
        $list = $this->field($field)
            ->where($map)
            ->order('sort')
            ->select();
        
        return list_to_tree($list, 'id', 'pid', '_');
    }
}
